package NextClass;


import NextClass.Native.*;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.TreeMap;

import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import vm.runtime.NextVMRuntime;

public class NativeClass implements Cloneable, Serializable {
    public String ClassName = null;
    public String[] Methods = null;
    public Object InitObject = null;

    public void Init(String InitClass, Object... InputObj) throws IOException {
        if (InitClass.equals("file")) {
            this.InitObject = new File(String.valueOf(InputObj[0]));
        }
        else if (InitClass.equals("http_service")) {
            String ipAddress = (String) InputObj[0];
            int port = Integer.parseInt(String.valueOf(InputObj[1]));

            // 创建HttpServer实例，并绑定到指定IP和端口
            this.InitObject = HttpServer.create(new InetSocketAddress(ipAddress, port), 0);
        }
        else if (InitClass.equals("Res")) {
            this.InitObject = (HttpExchange) InputObj[0];
        }
        else if (InitClass.equals("hashmap")) {
            this.InitObject = new HashMap<Object,Object>();
        }
        else if (InitClass.equals("requests")) {
            URL url = new URL((String) InputObj[0]);
            HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod((String) InputObj[1]);
            this.InitObject = httpURLConnection;
        }
        else if (InitClass.equals("treemap")) {
            this.InitObject = new TreeMap<Object,Object>();
        }
    }

    public Object run(String InvokeMethod, NextVMRuntime nextVMRuntime, Object... objects) throws Exception{
        if (this.ClassName.equals("file")) {
            return NextFile.run(InvokeMethod,nextVMRuntime,InitObject,objects);
        }
        else if (this.ClassName.equals("http_service")) {
            return HttpService.run(InvokeMethod,InitObject,nextVMRuntime,objects);
        }
        else if (this.ClassName.equals("Res")) {
            return Res.run(InvokeMethod,InitObject,nextVMRuntime,objects);
        }
        else if (this.ClassName.equals("hashmap")) {
            return NextHashmap.run(InvokeMethod,InitObject,nextVMRuntime,objects);
        }
        else if (this.ClassName.equals("requests")) {
            return Requests.run(InvokeMethod,InitObject,nextVMRuntime,objects);
        }
        else if (this.ClassName.equals("treemap")) {
            return NextTreeMap.run(InvokeMethod,InitObject,nextVMRuntime,objects);
        }
        return null;
    }


    @Override
    public Object clone() throws CloneNotSupportedException {
        // 创建一个新的NativeClass实例，并复制所有属性
        // 如果有其他非基本类型属性，也应当进行深拷贝
        // clone.otherField = (OtherClass) this.otherField.clone();
        return (NativeClass) super.clone();
    }
}
